
export function getClickable(node: UiObject | undefined): UiObject | undefined {
    if (!node) return;
    if (!node.clickable) return;
    if (node.clickable()) return node;
    const parent = node.parent()
    if (parent) return getClickable(parent)
    return node;
}

export function getCheckable(node: UiObject | undefined): UiObject | undefined {
    if (!node) return;
    if (node.checkable()) return node;
    const parent = node.parent()
    if (parent) return getCheckable(parent)
    return node;
}

export function getDisplaySize(doNotForcePortrait: boolean = false) {
    let { width, height } = device;
    if (width == 0) {
        // console.warn('AutoX.js获取到的设备尺寸为0，可能会影响正常运行，可以尝试重启设备');
        let metrics = (context as any).getResources().getDisplayMetrics();
        width = metrics.widthPixels;
        height = metrics.heightPixels;
    }
    if (doNotForcePortrait)
        return [width, height]
    return [
        Math.min(width, height),
        Math.max(width, height)
    ];
}

export function isInCurrentView(node: UiObject, isAll: boolean = false) {
    const [width, height] = getDisplaySize()
    const bounds = node.bounds()
    const left = bounds.left
    const right = bounds.right
    const top = bounds.top
    const bottom = bounds.bottom
    // 在屏幕中
    const isLeftTop = (left >= 0 && left < width) && (top >= 0 && top < height)
    const isRightBottom = (right <= width && right > 0) && (bottom > 0 && bottom <= height)
    if (isAll) {
        return isLeftTop && isRightBottom
    } else {
        return isLeftTop || isRightBottom;
    }
}
export function getCurrentViewNodes(nodesFac: () => UiSelector) {
    let items: UiObject[] = [];
    const nodes = nodesFac().find()
    for (let i = 0; i < nodes.size(); i++) {
        const node = nodes.get(i);
        if (node.visibleToUser()) {
            items.push(node);
        }
    }
    return items;
}

export function getCurrentViewNode(nodesFac: () => UiSelector, index: number = 0) {
    let items: UiObject[] = getCurrentViewNodes(nodesFac)
    if (index >= items.length - 1) {
        return items[items.length - 1]
    }
    return items[index];
}


export function getCurrentViewNodeUntil(nodesFac: () => UiSelector, timeout: number = 3000) {
    let time = new Date().getTime();
    let node = getCurrentViewNode(nodesFac)
    while (!node && new Date().getTime() - time <= timeout) {
        node = getCurrentViewNode(nodesFac)
    }
    return node;
}



export function getCurrentViewNodeUntilFind(nodesFac: () => UiSelector) {
    let node = getCurrentViewNode(nodesFac)
    while (!node && new Date().getTime()) {
        node = getCurrentViewNode(nodesFac)
    }
    return node;
}


export function clickButton(button?: UiObject) {
    if (!button) return false;
    const clickableButton = getClickable(button)
    if (clickableButton) {
        return clickableButton.click()
    }
    return false;
}

export function clickButtonOrRect(button?: UiObject) {
    if (!button) return false;
    if (button.clickable()) return button.click()
    return clickButtonRect(button)
}

export function clickButtonRect(button?: UiObject) {
    if (!button) return false;
    const bounds = button.bounds();
    press(bounds.centerX(), bounds.centerY(), random(50, 60));
    return true;
}

export function doubleClick(time: number = 10) {
    let i = time;
    while (i > 0) {
        click(device.width / 3, device.height * 5 / 11)
        sleep(30)
        click(device.width / 3, device.height * 5 / 11)
        sleep(50)
        i -= 1;
    }
}
